/**
 * Project Name:gc.sms.mvn
 * File Name:MenuServiceImpl.java
 * Package Name:com.clps.dev.sms.service.impl
 * Date:2018年11月14日下午1:59:07
 * Copyright (c) 2018, longbiosphere@163.com All Rights Reserved.
 *
 */
package com.clps.dev.sms.service.sm.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.clps.dev.sms.common.result.ResponseResult;
import com.clps.dev.sms.common.result.ResultType;
import com.clps.dev.sms.common.service.impl.BaseServiceImpl;
import com.clps.dev.sms.dao.sm.MenuDao;
import com.clps.dev.sms.entity.sm.Menu;
import com.clps.dev.sms.service.sm.IMenuService;
import com.clps.dev.sms.util.result.ResponseResultUtil;
import com.clps.dev.sms.vo.sm.MenuVO;

/**
 * ClassName: MenuServiceImpl <br/>
 * Description: . <br/>
 * <br/>
 * date: 2018年11月14日 下午1:59:07 <br/>
 *
 * @author long.ma
 * @version V1.0
 * @since JDK 1.8
 */

@Service
@Transactional
public class MenuServiceImpl extends BaseServiceImpl<Menu> implements IMenuService {

	/**
	 * Creates a new instance of MenuServiceImpl.
	 *
	 * @param clazz
	 */
	
	public MenuServiceImpl( ) {
		super(Menu.class);
	}

	private Logger logger = Logger.getLogger(MenuServiceImpl.class);

	@Autowired
	private MenuDao menuDao;

	/**
	 *
	 * @see com.clps.dev.sms.service.sm.IMenuService#addMenu(com.clps.dev.sms.entity.sm.Menu)
	 */
 
	
	@Override
	public ResponseResult<Menu> addMenu(Menu menu) {
		if (menu == null) {
			logger.error("menu不能为空");
			return ResponseResultUtil.error(ResultType.OBJECT_NULL);
		}
		if (menu.getMenuName() == null || menu.getMenuCreatedEmployeeId() == null || menu.getMenuCreatedTime() == null
				|| menu.getMenuUpdatedEmployeeId() == null) {
			logger.error("这个字段不能为空");
			return ResponseResultUtil.error(ResultType.INPUT_NULL);
		}
		List<Menu> list = menuDao.queryByMenuName(menu.getMenuName());
		if(0!=list.size()) {
			logger.error("这个字段必须唯一");
			return ResponseResultUtil.error(ResultType.CONTENT_REPEAT);
		}
		
		int i = menuDao.save(menu);
		if (i > 0) {
			return ResponseResultUtil.success(ResultType.ADD_SUCCESS);
		} else {
			return ResponseResultUtil.error(ResultType.ADD_FAULT);
		}
	}

	/**
	 *
	 * @see com.clps.dev.sms.service.sm.IMenuService#editMenu(com.clps.dev.sms.entity.sm.Menu)
	 */
	@Override
	public ResponseResult<Menu> editMenu(Menu menu) {
		
		if (menu == null) {
			logger.error("menu不能为空");
			return ResponseResultUtil.error(ResultType.OBJECT_NULL);
		}
		if (menu.getMenuName() == null || menu.getMenuCreatedEmployeeId() == null || menu.getMenuCreatedTime() == null
				|| menu.getMenuUpdatedEmployeeId() == null) {
			logger.error("这个字段不能为空");
			return ResponseResultUtil.error(ResultType.INPUT_NULL);
		}
		if(!menuDao.queryById(menu.getMenuId()).getMenuName().equals(menu.getMenuName())) {
			List<Menu> list = menuDao.queryByMenuName(menu.getMenuName());
			if(0!=list.size()) {
				logger.error("这个字段必须唯一");
				return ResponseResultUtil.error(ResultType.CONTENT_REPEAT);
			}
		}
		
		Menu menuQuery = menuDao.queryById(menu.getMenuId());
		if (!menuQuery.getMenuCreatedEmployeeId().equals(menu.getMenuCreatedEmployeeId())
				|| !menuQuery.getMenuCreatedTime().equals(menu.getMenuCreatedTime())) {
			logger.error("这个字段不能修改");
			return ResponseResultUtil.error(ResultType.CANNOT_CHANGE);
		}
		int i = menuDao.update(menu, menu.getMenuId());
		if (i > 0) {
			return ResponseResultUtil.success(ResultType.UPDATE_SUCCESS);
		} else {
			return ResponseResultUtil.error(ResultType.UPDATE_FAULT);
		}
	}

	/**
	 *
	 * @see com.clps.dev.sms.service.sm.IMenuService#queryMenu(java.lang.Integer)
	 */
	@Override
	public ResponseResult<Menu> queryMenu(Integer id) {
		Menu menu = menuDao.queryById(id);
		return ResponseResultUtil.success(ResultType.QUERY_SUCCESS,menu);
	}



	/**
	 *
	 * @see com.clps.dev.sms.service.sm.IMenuService#queryAllMenu()
	 */
	@Override
	public ResponseResult<Menu> queryAllMenu() {
		List<Menu> list = menuDao.queryALl();
		return ResponseResultUtil.success(ResultType.QUERY_SUCCESS,list);
	}
	
	/**
	 *
	 * @see com.clps.dev.sms.service.sm.IMenuService#queryAllMenuJson()
	 */
	
	
	@Override
	public ResponseResult<Menu> queryAllMenuVO() {
		List<MenuVO> list=menuDao.queryMenuVO();
		return ResponseResultUtil.success(ResultType.QUERY_SUCCESS,list);
	}

	 

	

}
